package shop.up.cart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import shop.up.cart.mapper.CartInfoMapper;
import shop.up.cart.service.CartAsyncService;
import shop.up.model.cart.CartInfo;

import javax.annotation.Resource;

/**
 * @author zzc
 * @date 2023/4/12 20:57
 */
@Service
public class CartAsyncServiceImpl implements CartAsyncService {
    @Resource
    private CartInfoMapper cartInfoMapper;

    /**
     * 异步修改购物车
     *
     * @param cartInfo
     */
    @Async
    @Override
    public void updateCartInfo(CartInfo cartInfo) {
        QueryWrapper<CartInfo> cartInfoQueryWrapper = new QueryWrapper<>();
        cartInfoQueryWrapper.eq("user_id", cartInfo.getUserId());
        cartInfoQueryWrapper.eq("sku_id", cartInfo.getSkuId());
        //  更新数据
        cartInfoMapper.update(cartInfo, cartInfoQueryWrapper);
    }

    /**
     * 异步保存购物车
     *
     * @param cartInfo
     */
    @Async
    @Override
    public void saveCartInfo(CartInfo cartInfo) {
        cartInfoMapper.insert(cartInfo);
    }

    /**
     * 异步删除临时用户购物车
     *
     * @param userId
     */
    @Async
    @Override
    public void deleteCartInfo(String userId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>().eq("user_id", userId));
    }

    /**
     * 选中状态变更
     *
     * @param userId
     * @param isChecked
     * @param skuId
     */
    @Async
    @Override
    public void checkCart(String userId, Integer isChecked, Long skuId) {
        CartInfo cartInfo = new CartInfo();
        cartInfo.setIsChecked(isChecked);
        QueryWrapper<CartInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("sku_id", skuId);
        cartInfoMapper.update(cartInfo, queryWrapper);
    }

    /**
     * 删除登录用户购物车中指定skuId的商品
     *
     * @param userId
     * @param skuId
     */
    @Async
    @Override
    public void deleteCartInfo(String userId, Long skuId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>()
                .eq("user_id", userId).eq("sku_id", skuId));
    }
}
